<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cjrtnc.leaningtech.com/4.2/loader.js"></script>
    <title>Document</title>
</head>

<body>
    <script>
        async function downloadFileToCheerpJ() {
            const response = await fetch("temp.jar");
            const reader = response.body.getReader();
            const contentLength = +response.headers.get('Content-Length');

            const bytes = new Uint8Array(contentLength);

            let pos = 0;
            while (true) {
                const { done, value } = await reader.read();
                if (done)
                    break;
                bytes.set(value, pos);
                pos += value.length;
            }

            // Write to CheerpJ filesystem
            return new Promise((resolve, reject) => {
                var fds = [];
                cheerpOSOpen(fds, "/files/temp.jar", "w", fd => {
                    cheerpOSWrite(fds, fd, bytes, 0, bytes.length, w => {
                        console.log(resolve)
                        cheerpOSClose(fds, fd, resolve);
                    });
                });
            });
        }

        ((async () => {
            await cheerpjInit({
                version: 17
            });
            cheerpjCreateDisplay(600, 900)
            await downloadFileToCheerpJ()
            cheerpjRunJar("/files/temp.jar")
        })())
    </script>
</body>

</html>